﻿using Common.Communal;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Product.API.Write.Applications.Commands.Products;

namespace Product.API.Write.Controllers
{
    /// <summary>
    /// 产品API-写
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        private readonly ILogger<ProductController> logger;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        public ProductController(IMediator mediator,ILogger<ProductController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }


        /// <summary>
        /// 产品添加
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回任务</returns>
        [HttpPost("CreateProduct")]
        public async Task<ApiResult<int>> CreateProduct(CreateProductCommand command)
        {
            return await mediator.Send(command);
        }


        /// <summary>
        /// 产品修改
        /// </summary>
        /// <param name="command">请求</param>
        /// <returns>返回任务</returns>
        [HttpPost("UpdateProduct")]
        public async Task<ApiResult<int>> UpdateProduct(UpdateProductCommand command)
        {
            return await mediator.Send(command);
        }


        /// <summary>
        /// 产品删除
        /// </summary>
        /// <param name="Id">主键</param>
        /// <returns>返回任务</returns>
        [HttpGet("DeleteProduct")]
        public async Task<ApiResult<int>> DeleteProduct(int Id)
        {
            logger.LogInformation("产品删除成功");
            DeleteProductCommand command = new DeleteProductCommand();
            command.Id = Id;
            return await mediator.Send(command);
        }



    }
}
